package com.glsc.ngateway.opmanage.service.performance;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.performance.DayPerformanceItem;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.performance.DayPerformanceItemRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class DayPerformanceItemService {

    @Resource
    private DayPerformanceItemRepository dayPerformanceItemRepository;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<DayPerformanceItem> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<DayPerformanceItem> page = dayPerformanceItemRepository.findAll(SpecificationUtil.buildSpecification(param, DayPerformanceItem.class), pageable);

        return page;
    }

    /**
     * 根据用户姓名和日期查询
     *
     * @param userName
     * @param date
     * @return
     */
    public List<DayPerformanceItem> findByUserNameAndDate(String userName, String date) {
        if (StringUtils.isBlank(date) || StringUtils.isBlank(userName)) {
            return null;
        }
        List<DayPerformanceItem> list = dayPerformanceItemRepository.findAll(Example.of(DayPerformanceItem.builder().userName(userName).effectDate(date).build()));
        return list;
    }

    /**
     * 根据岗位名称和日期查询
     *
     * @param postName
     * @param date
     * @return
     */
    public List<DayPerformanceItem> findByPostNameAndDate(String postName, String date) {
        if (StringUtils.isBlank(date) || StringUtils.isBlank(postName)) {
            return null;
        }
        List<DayPerformanceItem> list = dayPerformanceItemRepository.findAll(Example.of(DayPerformanceItem.builder().postName(postName).effectDate(date).build()));
        return list;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public DayPerformanceItem findById(Integer id) {
        if (Objects.isNull(id)) {
            return null;
        }
        Optional<DayPerformanceItem> optional = dayPerformanceItemRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public DayPerformanceItem save(DayPerformanceItem obj) {
        if (Objects.isNull(obj)) {
            return null;
        }
        return dayPerformanceItemRepository.save(obj);
    }


    public Integer findUserSumManHour(String userOaAccount, LocalDateTime startTime, LocalDateTime endTime, String ledgerType, String postName) {
        return dayPerformanceItemRepository.findUserSumManHour(userOaAccount, startTime, endTime, ledgerType, postName);
    }


}
